package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceReoprtDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备表Controller
 *
 * @author jby
 * @date 2025-08-21
 */
@Api(tags = "设备表管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController {

    @Autowired
    private IDeviceService deviceService;


    /**
     * 查询产品详情(报警规则模块使用)
     *
     * @param
     * @return
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("查询所有产品列表")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey) {
        return R.ok(deviceService.queryProduct(productKey));
    }

    /**
     * 设备同步
     *
     * @param
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation("同步设备列表")
    public AjaxResult asycDevice() {
        deviceService.asycDevice();
        return success();
    }

    /**
     * 分页查询设备列表
     *
     * @param
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public R list() {
        return R.ok(deviceService.allProduct());
    }

    /**
     * 设备注册
     *
     * @param
     * @return
     */
    //todo 注册失败, 数据不全
    @PostMapping("/register")
    @ApiOperation("设备注册")
    public R register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return R.ok();
    }

    /**
     * 查询设备表列表
     */
    @ApiOperation("查询设备表列表")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备表查询条件") Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 获取设备详细信息
     */
    @ApiOperation("获取设备详细信息")
    @GetMapping("/{iotId}")
    public R getByIotId(@PathVariable String iotId) {
        return R.ok(deviceService.getByIotId(iotId));
    }

    /**
     * 获取设备详细信息
     */
    @ApiOperation("获取设备详细信息")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceReoprtDataVo>> queryServiceProperties(@PathVariable String iotId) {
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /*
    * 修改设备
    * */
    @ApiOperation("修改设备")
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     * @param
     * @return
     */
    @ApiOperation("删除设备")
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return success();
    }
}
